package com.ruoyi.maintain.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.car.api.RemoteCarService;
import com.ruoyi.car.api.domain.CheCar;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.user.api.RemoteUsersService;
import com.ruoyi.user.api.domain.CheUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.maintain.domain.CheMaintain;
import com.ruoyi.maintain.service.ICheMaintainService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 维修任务Controller
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/maintain")
public class CheMaintainController extends BaseController
{
    @Autowired
    private ICheMaintainService cheMaintainService;
    @Autowired
    private RemoteUsersService  remoteUsersService;
    @Autowired
    private RemoteCarService remoteCarService;

    /**
     * 条件查询任务列表
     */
//    @RequiresPermissions("maintain:maintain:list")
    @GetMapping("/cheMaintainList")
    public AjaxResult cheMaintainList(CheMaintain cheMaintain){
        List<CheMaintain> list = cheMaintainService.selectCheMaintains(cheMaintain);
        for (CheMaintain cheMaintain1 : list) {
            Long userId = cheMaintain1.getUserId();
            AjaxResult info = remoteUsersService.getInfo(userId, SecurityConstants.INNER);
            CheUser data = JSON.parseObject(JSON.toJSONString(info.get("data")), CheUser.class);
            cheMaintain1.setCheUser(data);


            Long carId1 = cheMaintain1.getCarId();
            CheCar byId1 = remoteCarService.getById(carId1, SecurityConstants.INNER);
            cheMaintain1.setCheCar(byId1);

        }
        return success(list);
    }

    /**
     * 查询维修任务列表
     */
    @RequiresPermissions("maintain:maintain:list")
    @GetMapping("/list")
    public TableDataInfo list(CheMaintain cheMaintain)
    {
        startPage();
        List<CheMaintain> list = cheMaintainService.selectCheMaintainList(cheMaintain);
        for (CheMaintain cheMaintain1 : list){
             Long userId = cheMaintain1.getUserId();
             AjaxResult info = remoteUsersService.getInfo(userId, SecurityConstants.INNER);
             CheUser data = JSON.parseObject(JSON.toJSONString(info.get("data")), CheUser.class);
             cheMaintain1.setCheUser(data);



            Long carId1 = cheMaintain1.getCarId();
            CheCar byId1 = remoteCarService.getById(carId1, SecurityConstants.INNER);
            cheMaintain1.setCheCar(byId1);

        }



        return getDataTable(list);
    }

    /**
     * 导出维修任务列表
     */
    @RequiresPermissions("maintain:maintain:export")
    @Log(title = "维修任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheMaintain cheMaintain)
    {
        List<CheMaintain> list = cheMaintainService.selectCheMaintainList(cheMaintain);
        ExcelUtil<CheMaintain> util = new ExcelUtil<CheMaintain>(CheMaintain.class);
        util.exportExcel(response, list, "维修任务数据");
    }

    /**
     * 获取维修任务详细信息
     */
    @RequiresPermissions("maintain:maintain:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheMaintainService.selectCheMaintainById(id));
    }

    /**
     * 新增维修任务
     */
    @RequiresPermissions("maintain:maintain:add")
    @Log(title = "维修任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheMaintain cheMaintain)
    {
        return toAjax(cheMaintainService.insertCheMaintain(cheMaintain));
    }

    /**
     * 修改维修任务
     */
    @RequiresPermissions("maintain:maintain:edit")
    @Log(title = "维修任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheMaintain cheMaintain)
    {
        return toAjax(cheMaintainService.updateCheMaintain(cheMaintain));
    }

    /**
     * 删除维修任务
     */
    @RequiresPermissions("maintain:maintain:remove")
    @Log(title = "维修任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheMaintainService.deleteCheMaintainByIds(ids));
    }
}
